#include "pmr.h"
#include "rectype.h"
#include "util.h"
#include "cnfield.h"
#include "nmfield.h"

using namespace std;
using namespace opsrc::stdf;

Pmr::Pmr()
{
}

Pmr::Pmr(stdf_rec _stdfrec, int _reclen, std::ios::pos_type _fileRecFirstBytePosition, int _orderindex, endian e, char* buf)
:RecBase(_stdfrec, _reclen, _fileRecFirstBytePosition, _orderindex), en(e) {
    if (buf != 0)
        readFields(buf);
}

Pmr::~Pmr() {
}

void Pmr::setUniquePinIndex(StdfField<int> pmrindx) {
    pmr_indx = pmrindx;
}

const StdfField<int> Pmr::getUniquePinIndex() const {
    return pmr_indx;
}

void Pmr::setChannelType(StdfField<int> chantyp) {
    chan_typ = chantyp;
}

const StdfField<int> Pmr::getChannelType() const {
    return chan_typ;
}

void Pmr::setChannelName(StdfField<string> channam) {
    chan_nam = channam;
}

const StdfField<string> Pmr::getChannelName() const {
    return chan_nam;
}

void Pmr::setPhysicalPinName(StdfField<string> phynam) {
    phy_nam = phynam;
}

const StdfField<string> Pmr::getPhysicalPinName() const {
    return phy_nam;
}

void Pmr::setLogicalPinName(StdfField<string> lognam) {
    log_nam = lognam;
}

const StdfField<string> Pmr::getLogicalPinName() const {
    return log_nam;
}

const StdfField<int> Pmr::getHeadNum() const {
    return head_num;
}

void Pmr::setHeadNum(StdfField<int> headnum) {
    head_num = headnum;
}

const StdfField<int> Pmr::getSiteNum() const {
    return site_num;
}

void Pmr::setSiteNum(StdfField<int> sitenum) {
    site_num = sitenum;
}

void Pmr::readFields(char *buf) {

#if _DEBUG
    cout << "stdfrec= PMR , filebytestartposition= " <<   getFileRecByteStartPosition() << ", recbytecount= " << reclen <<endl;
#endif
    // read unique index, 2 bytes
    int curpos = 0;
    NmField<unsigned int, 2> pmrindx(buf+curpos, en);
    pmr_indx.set(pmrindx.getData(), curpos, pmrindx.getByteCount(), pmrindx.getTotalByteCount());
    curpos += pmr_indx.getTotalByteCount();

    // Read Channel type, 2 bytes
    if (curpos < reclen-1 ) {
        NmField<unsigned int, 2> chantyp(buf + curpos, en);
        chan_typ.set(chantyp.getData(), curpos, chantyp.getByteCount(), chantyp.getTotalByteCount());
        curpos += chan_typ.getTotalByteCount();
    }

    // Read Channel name,
    if (curpos < reclen-1) {
        CnField<string> channam(buf + curpos, en);
        chan_nam.set(channam.getData(), curpos, channam.getByteCount(), channam.getTotalByteCount());
        curpos += chan_nam.getTotalByteCount();
    }

    // Read Channel Physical name,
    if (curpos < reclen-1) {
        CnField<string> phynam(buf + curpos, en);
        phy_nam.set(phynam.getData(), curpos, phynam.getByteCount(), phynam.getTotalByteCount());
        curpos += phy_nam.getTotalByteCount();
    }

    // Read Channel logical name,
    if (curpos <reclen-1) {
        CnField<string> lognam(buf + curpos, en);
        log_nam.set(lognam.getData(), curpos, lognam.getByteCount(), lognam.getTotalByteCount());
        curpos += log_nam.getTotalByteCount();
    }

    // Read head number , 1 byte
    if (curpos < reclen-1) {
        NmField<unsigned int, 1> headnum(buf+curpos, en);
        head_num.set(headnum.getData(), curpos, headnum.getByteCount(), headnum.getTotalByteCount());
        curpos += head_num.getTotalByteCount();
    }


    // Read head number , 1 byte
    if (curpos < reclen-1) {
        NmField<unsigned int, 1> sitenum(buf+curpos, en);
        site_num.set(sitenum.getData(), curpos, sitenum.getByteCount(), sitenum.getTotalByteCount());
    }

}

